/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableSoftware;
import neptune.web.client.view.input.DefaultInputValidator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class SoftwarePropsDialog extends DialogBox {

	private TreeItem treeItem;
	private FlexTable mainPanel;
	private TextBox name;
	private TextBox type;
	private TextBox version;
	private ClickListener okListener = new ClickListener() {
		public void onClick(Widget sender) {
			if (validateInputs()) {
				software.setId(name.getText());
				software.setType(type.getText());
				software.setVersion(version.getText());

				SoftwareMenuBar widget = (SoftwareMenuBar) treeItem.getWidget();
				widget.getRootItem().setText(software.getId());
				SoftwarePropsDialog.this.hide();
			}
		}
	};
	private ClickListener cancelListener = new ClickListener() {
		public void onClick(Widget sender) {
			if(cancelCallBack != null)
				cancelCallBack.onCancel();
			SoftwarePropsDialog.this.hide();
		}
	};
	private SerializableSoftware software;
	private CancelCallBack cancelCallBack;

	public SoftwarePropsDialog(SerializableSoftware software, TreeItem treeItem) {
		super(false, true);

		this.treeItem = treeItem;

		mainPanel = new FlexTable();
		mainPanel.setWidth("95%");
		ScrollPanel mainPanelWrapper = new ScrollPanel(mainPanel);
		mainPanelWrapper.setHeight("200px");

		/*
		 * Inizio costruzione campi per inserimemnto dati
		 */
		// NAME
		Label nameLabel = new Label("Software Name");
		nameLabel.setStylePrimaryName("NodePropsDialog-label");
		name = new TextBox();
		name.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(0, 0, nameLabel);
		mainPanel.setWidget(0, 1, name);

		// TYPE
		Label typeLabel = new Label("Software Type");
		typeLabel.setStylePrimaryName("NodePropsDialog-label");
		type = new TextBox();
		type.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(1, 0, typeLabel);
		mainPanel.setWidget(1, 1, type);

		// VERSION
		Label versionLabel = new Label("Software Version");
		versionLabel.setStylePrimaryName("NodePropsDialog-label");
		version = new TextBox();
		version.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(2, 0, versionLabel);
		mainPanel.setWidget(2, 1, version);

		// BUTTON PANEL
		HorizontalPanel buttonPanel = new HorizontalPanel();
		Button ok = new Button("Ok");
		ok.addClickListener(okListener);
		ok.setStylePrimaryName("NodePropsDialog-button");
		Button cancel = new Button("Cancel");
		cancel.addClickListener(cancelListener);
		cancel.setStylePrimaryName("NodePropsDialog-button");
		buttonPanel.add(ok);
		buttonPanel.add(cancel);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.add(mainPanelWrapper);
		vPanel.add(buttonPanel);
		vPanel.setCellHorizontalAlignment(buttonPanel,
				VerticalPanel.ALIGN_RIGHT);

		this.setWidget(vPanel);
		this.setText("Software Properties");

		// Styles
		mainPanelWrapper.addStyleName("NodePropsDialog-mainWrapper");
		mainPanel.addStyleName("NodePropsDialog-mainPanel");
		this.addStyleName("NodePropsDialog");

		setData(software);
	}

	public boolean validateInputs() {
		// Resetta le segnalazioni d'errore
		name.removeStyleName("NodePropsDialog-input-error");
		type.removeStyleName("NodePropsDialog-input-error");
		version.removeStyleName("NodePropsDialog-input-error");

		DefaultInputValidator defaultValidator = new DefaultInputValidator();

		boolean[] errors = new boolean[2];
		for (int i = 0; i < errors.length; i++)
			errors[i] = true;

		if (!defaultValidator.isValid(name.getText())) {
			errors[0] = false;
			name.addStyleName("NodePropsDialog-input-error");
		}
		if (!defaultValidator.isValid(type.getText())) {
			errors[0] = false;
			type.addStyleName("NodePropsDialog-input-error");
		}
		if (!defaultValidator.isValid(version.getText())) {
			errors[0] = false;
			version.addStyleName("NodePropsDialog-input-error");
		}

		for (int i = 0; i < errors.length; i++) {
			if (!errors[i])
				return false;
		}

		return true;
	}

	public void setData(SerializableSoftware soft) {
		name.setText(soft.getId());
		type.setText(soft.getType());
		version.setText(soft.getVersion());

		this.software = soft;
	}

	public void setCancelCallback(CancelCallBack cancelCallBack) {
		this.cancelCallBack = cancelCallBack;
	}

	/**
	 * @return the cancelCallBack
	 */
	public CancelCallBack getCancelCallBack() {
		return cancelCallBack;
	}

}
